#!/bin/bash

# 检查是否以 root 用户运行
if [ "$EUID" -ne 0 ]; then
    echo "请以 root 用户运行此脚本。"
    exit
fi

# 检查是否为 Debian 系统
if [ ! -f /etc/debian_version ]; then
    echo "此脚本仅适用于 WSL 下的 Debian 系统。"
    exit 1
fi

# 备份并更新 sources.list
mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.ustc.edu.cn/debian bookworm main contrib non-free non-free-firmware" >/etc/apt/sources.list

# 更新软件包列表并安装 curl 和 sudo
apt update
apt upgrade -y
apt install -y curl wget sudo locales

# 设置 root 密码
echo "请设置 root 密码。"
passwd root

# 创建新用户
read -p "请输入新用户名: " username
useradd $username -m -G sudo -s /bin/bash

# 设置用户密码
echo "请设置 $username 的密码。"
passwd "$username"

# 将新用户设置为wsl默认用户
echo -e "[user]\ndefault = $username" >>/etc/wsl.conf
echo -e "[boot]\nsystemd=true" | sudo tee -a /etc/wsl.conf

# 安装 chsrc 并查找最快的源
curl https://chsrc.run/posix | bash
chsrc set debian

# 安装常用软件
apt-get install -y vim git

# 询问是否安装docker
read -p "是否安装docker？(y/n): " install_docker
if [ "$install_docker" = "y" ]; then
    # 安装docker
    bash <(curl -sSL https://linuxmirrors.cn/docker.sh)
fi
